<%--
  Created by IntelliJ IDEA.
  User: Shuwei  Wang
  Date: 2020/6/18
  Time: 15:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
</head>
<body>

<div class="demoTable">
    用户姓名：
    <div class="layui-inline">
        <input class="layui-input" name="name"   type="text">
    </div>
    电话：
    <div class="layui-inline">
        <input class="layui-input" name="phone"   type="text">
    </div>
    <button class="layui-btn" <%--data-type="reload"--%> id="query">搜索</button>
</div>
<%--用户姓名<input name="name" class="layui-input-block"/>
<br>
电话<input type="text" name="phone"></input> <button class="layui-btn" id="query">查询</button>--%>
<button class="layui-btn" id="toAddGuru">添加用户</button>
<%--<form class="layui-form" action="" id="addUserForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">用户姓名</label>
        <div class="layui-input-block">
            <input type="text" name="userName" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">用户电话</label>
        <div class="layui-input-block">
            <input type="text" name="userTelphone" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <button type="button" class="layui-btn" id="test1">
        <i class="layui-icon">&#xe67c;</i>上传上师照片
    </button>
    <img src="" id="guruImage" >
    <input type="hidden" name="guruImage" >

    <div class="layui-form-item">
        <label class="layui-form-label">是否显示</label>
        <div class="layui-input-block">
            <input type="radio" name="guruStatus" value="1" title="是">
            <input type="radio" name="guruStatus" value="0" title="否">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="submituser">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>--%>


<%--修改上师--%>
<form class="layui-form" action="" lay-filter="update" id="updUserForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="userName" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <input type="hidden"  name="userId"/><%--编号隐藏--%>
    <div class="layui-form-item">
        <label class="layui-form-label">电话号码</label>
        <div class="layui-input-block">
            <input type="text" name="userTelphone" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="userPassword" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <button type="button" class="layui-btn" id="updtest1">
        <i class="layui-icon">&#xe67c;</i>修改照片
    </button>
    <img src="" id="upduserImage" >
    <input type="hidden" name="userImage" >

    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-block">
            <input type="text" name="userNickname" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="userSex" value="男" title="男" id="nan"/>
            <input type="radio" name="userSex" value="女" title="女" id="nv"/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">个性签名</label>
        <div class="layui-input-block">
            <input type="text" name="userAutograph" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">省份</label>
        <div class="layui-input-block">
            <input type="text" name="userProvince" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">城市</label>
        <div class="layui-input-block">
            <input type="text" name="userCity" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">所从上师</label>
        <div class="layui-input-block">
            <select name="guruId" lay-verify="required">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="userStatus" value="1" title="正常" id="shi"/>
            <input type="radio" name="userStatus" value="0" title="冻结" id="fou"/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">创建时间</label>
        <div class="layui-input-block">
            <input type="text" name="userCreateDate" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="upduser">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<table id="demo" lay-filter="test"></table>

<script src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/js/jquery-1.12.4.js"></script>
<script>
    //Demo
    layui.use(['form','table','layer','upload'], function(){
        var form = layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var upload=layui.upload;
        var index;
        var wsw;
        var html;
        table.render({
            elem: '#demo'
            ,height: 500
           ,/*where:{
                name:$("[name='name']").val(),
               phone:$("[name='phone']").val()
           }
            ,*/url: '${pageContext.request.contextPath}/user/selByNameOrPhone' //数据接口
            ,limit:5
            ,limits:[5,10]
            ,page: true //开启分页
           /* ,where:{name:"",
           phone:""}*/
            ,cols: [[ //表头
                {field: 'userId', title: '编号',  sort: true/*, fixed: 'left'*/}
                ,{field: 'userName', title: '名字'}
                ,{field: 'userStatus', title: '状态',  sort: true}

                ,{field: 'userNickname', title: '用户昵称'}
                ,{field:'userImage',title: '用户照片',  sort: true, width:224,height:224,templet:function (data) {
                        console.log(data.userImage)
                        return "<img src='${pageContext.request.contextPath}/img/"+data.userImage+"'/>";
                    }
                }
                ,{field: 'userSex', title: '性别'}
                ,{ field:'guruId',title: '所从上师',templet:function (data) {
                        $.ajax({
                            async:false,
                            url:'${pageContext.request.contextPath}/guru/selOneGuru',
                            data:"id="+data.guruId,
                            dataType:"json",
                            type:'post',
                            success:function (d) {
                               wsw=d.guruName;
                               //alert(guruName);
                            }
                        });

                        return wsw;

                    }}
                ,{field: 'userAutograph', title: '个性签名'}
                ,{field: 'userProvince', title: '省份'}
                ,{field: 'userCity', title: '城市'}
                ,{field: 'userCreateDate', title: '创建时间'}
                ,{field: 'userTelphone', title: '电话'}
                ,{title: '操作',toolbar: '#barDemo'}

            ]]
        });


        $("#query").click(function () {
           // name= $("[name='name']").val();
            //phone=$("[name='phone']").val();
           // alert($("[name='phone']").val()+$("[name='name']").val());
           table.reload('demo',{

               where:{
                   name:$("[name='name']").val()
                   ,phone:$("[name='phone']").val()
               }
               ,page:{
                   curr:1
               }
               ,height:500
            });
        });

        //监听工具条
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if(layEvent === 'detail'){ //查看
                //do somehing
            } else if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"${pageContext.request.contextPath}/user/delUser",
                        data:"id="+data.userId,
                        dataType:"text",
                        type:"post",
                        success:function (d) {
                            if(d=="true"){
                                layer.msg("下架成功！");
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                            }else{
                                layer.msg("操作失败！");
                                layer.close(index);
                            }
                        }
                    });


                    //向服务端发送删除指令
                });
            } else if(layEvent === 'edit'){ //编辑
                //do something
                $("#upduserImage").prop("src","${pageContext.request.contextPath}/img/"+data.userImage);
                $.ajax({
                    async:false,
                    url:'${pageContext.request.contextPath}/guru/allGuru',
                    dataType:"json",
                    type:"post",
                    success:function (d) {
                        html="<option>请选择上师</option>";

                        $.each(d,function (i,o) {
                           html+="<option value='"+o.guruId+"'>"+o.guruName+"</option>";
                        });
                       // alert(html);
                    }
                });
                $("[name='guruId']").html(html);
                form.val('update',data)
                <%--$.ajax({--%>
                <%--url:"${pageContext.request.contextPath}/guru/selOneGuru",--%>
                <%--data:"id="+data.guruId,--%>
                <%--dataType:"json",--%>
                <%--type:"post",--%>
                <%--success:function (d) {--%>
                <%--$("[name='guruId']").val(d.guruId);--%>
                <%--$("[name='guruName']").val(d.guruName);--%>
                <%--$("[name='guruImage']").val(d.guruImage);--%>
                <%--$("#updguruImage").prop("src","${pageContext.request.contextPath}/img/"+d.guruImage);--%>
                <%--$("[name='guruNickname']").val(d.guruNickname);--%>

                <%--if(d.guruStatus==1){--%>

                <%--/*$("#shi").prop("checked",true);*/--%>

                <%--$(document).on("#shi",function () {--%>
                <%--$(this).prop("checked","checked");--%>
                <%--});--%>

                <%--}else {--%>
                <%--$("#fou").prop("checked",true);--%>
                <%--}--%>


                <%--}--%>
                <%--});--%>

                index=layer.open({
                    type:1,
                    area:['500px','500px'],
                    content:$("#updUserForm"),
                    title:"修改用户信息"
                });
                form.on("submit(upduser)",function (data) {
                    var data=data.field;
                    $.ajax({
                        url:'${pageContext.request.contextPath}/user/updUser',
                        data:data,
                        dataType:'text',
                        type:"post",
                        success:function (data1) {
                            if(data1=='true'){
                                layer.msg('修改成功');
                                //同步更新缓存对应的
                                // $("#updGuruForm").get(0).reset();
                                table.reload("demo");

                                layer.close(index);
                            }else {
                                layer.msg('修改失败！');
                            }
                        }
                    })
                    return false;
                });


            } else if(layEvent === 'LAYTABLE_TIPS'){
                layer.alert('Hi，头部工具栏扩展的右侧图标。');
            }
        });

        //添加上传上师图片
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '${pageContext.request.contextPath}/user/upload' //上传接口
            ,done: function(res){
                //上传完毕回调
                if(res.code==0){
                    $("#guruImage").prop("src","${pageContext.request.contextPath}/img/"+res.src);
                    $("[name='guruImage']").val(res.src);
                    layer.msg("上传成功！");
                }
            }
            ,error: function(){
                //请求异常回调
                layer.msg('上传失败！');
            }
        });

        //修改上传用户图片

        var uploadInst1 = upload.render({
            elem: '#updtest1' //绑定元素
            ,url: '${pageContext.request.contextPath}/user/upload' //上传接口
            ,done: function(res){
                //上传完毕回调
                if(res.code==0){
                    $("#upduserImage").prop("src","${pageContext.request.contextPath}/img/"+res.src);
                    $("[name='userImage']").val(res.src);
                    layer.msg("修改成功！");
                }
            }
            ,error: function(){
                //请求异常回调
                layer.msg('修改失败！');
            }
        });

        $("#toAddUser").click(function () {
            index= layer.open({
                title:"添加上师",
                area:['400px','400px'],
                content:$("#addUserForm"),
                type:1
            })
        })
        //监听提交
       /* form.on('submit(submitguru)', function(data){

            layer.msg(JSON.stringify(data.field));
            var data=data.field;
            $.ajax({
                url:"/guru/addGuru"
                ,data:data
                ,dataType:"text"
                ,type:"post"
                ,success:function (data) {
                    if(data=="true"){
                        layer.msg("添加上师成功！");
                        layer.close(index);
                    }else{
                        layer.msg("添加失败！");
                    }
                }
            })
            return false;
        });*/
    });
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

    <!-- 这里同样支持 laytpl 语法，如： -->
    {{#  if(d.auth > 2){ }}
    <a class="layui-btn layui-btn-xs" lay-event="check">审核</a>
    {{#  } }}
</script>

<style type="text/css">
    .layui-table-cell{
        text-align:center;
        height: auto;
        white-space: normal;
    }
</style>
</body>
</html>

